package org.example.classtopia.Service.Impl;


import lombok.extern.slf4j.Slf4j;
import org.example.classtopia.Common.Result.Result;
import org.example.classtopia.Mapper.ManagerMapper;
import org.example.classtopia.Mapper.PostMapper;
import org.example.classtopia.Mapper.UserMapper;
import org.example.classtopia.Model.Entity.Post;
import org.example.classtopia.Model.Entity.User;
import org.example.classtopia.Model.Entity.Resource;
import org.example.classtopia.Service.ManagerService;
import org.example.classtopia.Model.vo.PageResultVO;
import org.example.classtopia.Model.vo.PostPreviewVO;
import org.example.classtopia.Model.vo.ResourceDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.classtopia.Mapper.ResourceMapper;
import org.example.classtopia.Mapper.ReportMapper;

import java.util.List;

@Slf4j
@Service

public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private org.example.classtopia.Mapper.ResourceMapper resourceMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Override
    //id是要封禁的用户id，role是封禁者的role
    public Result BanUser(Long id, Integer role){
        if(role == 0){
            return Result.error(400,"无权访问！");
        }
        User user = userMapper.FindDataByID(Math.toIntExact(id));
        if(user == null){
            return Result.error(400,"该用户不存在！");
        }
        if(user.getStatus() == 1){
            return Result.error(400,"该用户已经被封禁！");
        }
        managerMapper.banUser(user.getId());
        return Result.success("封禁用户成功！", 200);
    }

    @Override
    public Result BanPost(Long postId, Integer role){
        if(role == 0){
            return Result.error(400,"无权访问！");
        }
        Post post = postMapper.selectPostById(postId);
        if(post == null){
            return Result.error(400,"帖子不存在！");
        }
        if(post.getStatus() == 1){
            return Result.error(400,"帖子已经被删除！");
        }
        if(post.getStatus() == 2){
            return Result.error(400,"帖子已经被屏蔽！");
        }

        managerMapper.banPost(postId);
        return Result.success("帖子屏蔽成功！",200);
    }

    @Override
    public boolean changeUserState(Long userId, Integer state) {
        User user = userMapper.FindDataByID(userId.intValue());
        if (user == null) {
            return false;
        }
        userMapper.UpdateUserStatus(userId, state);
        return true;
    }

    @Override
    public boolean changePostState(Long postId, Integer state) {
        Post post = postMapper.selectPostById(postId);
        if (post == null) {
            return false;
        }
        postMapper.updatePostStatus(postId, state);
        return true;
    }

    @Override
    public boolean changeResourceState(Long resourceId, Integer state) {
        Resource resource = resourceMapper.selectResourceById(resourceId);
        if (resource == null) {
            return false;
        }
        resourceMapper.updateResourceStatus(resourceId, state);
        return true;
    }

    @Override
    public PageResultVO<PostPreviewVO> getAllPostsFlexible(Integer status, Long categoryId, int offset, int size) {
        List<PostPreviewVO> list = postMapper.getPostPreviewsFlexible(status, categoryId, offset, size);
        long total = postMapper.countPostPreviewsFlexible(status, categoryId);
        int totalPages = (int) ((total + size - 1) / size);
        PageResultVO<PostPreviewVO> pageResult = new PageResultVO<>();
        pageResult.setRecords(list);
        pageResult.setPageNum(offset / size);
        pageResult.setPageSize(size);
        pageResult.setTotal(total);
        pageResult.setTotalPages(totalPages);
        return pageResult;
    }

    @Override
    public PageResultVO<ResourceDetailVO> getAllResourcesFlexible(Long categoryId, String fileType, Integer state, int offset, int size) {
        // 伪代码：请根据实际数据库和Mapper实现
        List<ResourceDetailVO> list = resourceMapper.getResourceDetailsFlexible(categoryId, fileType, state, offset, size);
        long total = resourceMapper.countResourceDetailsFlexible(categoryId, fileType, state);
        int totalPages = (int) ((total + size - 1) / size);
        PageResultVO<ResourceDetailVO> pageResult = new PageResultVO<>();
        pageResult.setRecords(list);
        pageResult.setPageNum(offset / size);
        pageResult.setPageSize(size);
        pageResult.setTotal(total);
        pageResult.setTotalPages(totalPages);
        return pageResult;
    }

    @Override
    public long countAllUsers() {
        return userMapper.countAllUsers();
    }

    @Override
    public long countAllPosts() {
        // postMapper.countAllPosts() 查询所有帖子总数
        return postMapper.countAllPosts();
    }

    @Override
    public long countAllResources() {
        // resourceMapper.countResourceDetailsFlexible(null, null, null) 查询所有资源总数
        return resourceMapper.countResourceDetailsFlexible(null, null, null);
    }

    @Override
    public long countPendingReports() {
        // "PENDING" 假设为待处理举报的状态
        return reportMapper.countReports("PENDING", null);
    }
}
